import 'dart:async';

import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/models/goldRecordModel.dart';

import '../blocProvider.dart';

/// 金币记录bloc
class GoldRecordBloc implements BlocBase {

  bool isFirstLoading = true;
  int page = 0;
  final int limit = 10;
  List<GoldRecordModel> lists = [];

  final List<GoldRecordModel> defaultData = [];
  final String token;

  GoldRecordBloc(this.token) {
    getGoldRecordList();
  }

  @override
  void dispose() {
    _goldRecords.close();
  }

// Stream ----------------------------------------------------------------------
  /// 金币记录列表
  StreamController<List<GoldRecordModel>> _goldRecords = StreamController.broadcast();
  Sink<List<GoldRecordModel>> get goldRecordsSink => _goldRecords.sink;
  Stream<List<GoldRecordModel>> get goldRecordsStream => _goldRecords.stream;

// method ----------------------------------------------------------------------
  /// 获取金币记录列表
  void getGoldRecordList() async {
    page += 1;
    List<GoldRecordModel> result = await UserApis.getGoldRecordList(token, page, limit);
    if (result.isNotEmpty) {
      isFirstLoading = false;
      lists.addAll(result);
      goldRecordsSink.add(lists);
    } else {
      if (!isFirstLoading) showToast("已经没有了喔~ww");
      isFirstLoading = false;
      goldRecordsSink.add(lists);
    }
  }

  /// 刷新
  void onRefresh() {
    page = 0;
    lists = [];
    getGoldRecordList();
  }

}